// 操作人ID
//var operateUserId = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID;
//操作人姓名
//var operateUserName = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name;
/*二级场景id获取*/
if(getQueryString("subSceneId")){
    var subSceneId = getQueryString("subSceneId");
}
if(getQueryString("id")){
    var secondId = getQueryString("id")
}
// 接口参数
var params = {
    appSceneId:secondId,//场景ID
    name:"",//锚点名称
    pageNum:1,
    pageSize:10,
}
var dataSource = [];
// 获取接口数据
var getData = {
    // 获取锚点列表数据
    sceneAnchorListsInfo:function(){
        var successData = function(data){
            if(data.errorCode == 0){
                if(!!data.data && data.data.result.length > 0){
                    renderData.renderSceneAnchorLists(data.data)
                    $(".M-box3").pagination({
                        pageCount: data.data.totalPage,
                        jump: true,
                        coping: true,
                        prevContent: '上页',
                        nextContent: '下页',
                        callback:function(api){
                            params.pageNum = api.getCurrent()
                            var successData = function(data){
                                renderData.renderSceneAnchorLists(data.data)
                            }
                            return web.doAjax(root.sroreinter.queryAppScenePoint, 'post', params, successData,2)
                        }
                    })
                } else {
                    $(".M-box3").pagination({
                        pageCount:0
                    })
                    renderData.renderSceneAnchorLists(data.data)
                }
            }else{
                $.Huimodalalert('数据异常',2000);
            }

        }
        return web.doAjax(root.sroreinter.queryAppScenePoint, 'post', params, successData,2);
    },
    //删除锚点
    deleteAnchorInfo:function(id){
        var params = {
            id:id
        }
        var successData = function(data){
            if(data.errorCode == 0){
                $.Huimodalalert('删除成功',2000);
                getData.sceneAnchorListsInfo();
            }
        }
        return web.doAjax(root.sroreinter.deleteById, 'post', params, successData,2)
    }
}
// 数据渲染
var renderData = {
    // 渲染列表数据
    renderSceneAnchorLists:function(data){
        dataSource = [];
        if(!!data && data.result.length >0){
            $.each(data.result,function(index,item){
                var obj = {};
                obj.name = item.name;// 锚点名称
                obj.coordinate = item.coordinate; //位置
                obj.productType  = item.productType;// 锚点类型
                obj.createTime = item.createTime; // 创建时间
                obj.updateTime = item.updateTime; // 修改时间
                obj.id = item.id ;//锚点唯一标识
                obj.itemData = item;
                dataSource.push(obj)
            })
        }
        renderData.anchorLists(dataSource)
    },
    // 表格渲染
    anchorLists:function(data){
        var datatable = $("#sceneEditList").dataTable(); // 初始化dataTable，清空上一个值
        datatable.fnClearTable();//清空一下table
        datatable.fnDestroy(); //还原初始化了的datatable
        $("#sceneEditList").DataTable({
            "order": [[ 1, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据"
            },
            data:data,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var name = row.name;
                        if(name == undefined){
                            name = ''
                        }
                        return '<span style="text-align: center;" >'+name+'</span>';
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var coordinate = row.coordinate;
                        if(coordinate == undefined){
                            coordinate = ''
                        }
                        return  '<span style="text-align: center;" >'+ coordinate +'</span>';

                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row, meta) {
                        var productType = row.productType;
                        switch (productType) {
                            case '10':
                                return productType = '商品类型';
                            case '20':
                                return productType = 'spu';
                            default:
                                return productType = '';
                        }

                        return  '<span style="text-align: center;">'+ productType +'</span>';
                    },
                    "targets": 2
                },
                {
                    "render": function(data, type, row, meta) {
                        var operateTime = '';// 操作时间
                        var createTime = row.createTime;
                        var updateTime = row.updateTime;
                        if(!updateTime){
                            operateTime = createTime;
                        }else{
                            operateTime = updateTime;
                        }
                        return  '<span style="text-align: center;">'+ operateTime +'</span>';
                    },
                    "targets": 3
                },
                {
                    "render": function(data, type, row, meta) {
                        return "<span style='text-align: center; color: blue; margin-right: 10px; cursor: pointer' onclick='modify("+row.id+",this)' data-itemData='"+JSON.stringify(row.itemData)+"'>修改</span><span style='text-align: center; color: #0000ff;cursor: pointer' onclick='cancel("+row.id+")'>删除</span>";
                    },
                    "targets": 4
                },
            ],
        })
    }
}
getData.sceneAnchorListsInfo();
// 逻辑处理
// 查询
function search(){
    params.name = $("#sceneName").val();
    getData.sceneAnchorListsInfo();
}
//新增
function add(){
   // 跳转锚点新增页面
   var singleUrl = layer.open({
        type: 2,
        title: "新增锚点",
        content: 'sceneAnchorManagement.html?subSceneId='+subSceneId+'&operateType=add'+'&secondId='+secondId,
        end:function(){
            location.reload()
        }
    })
    layer.full(singleUrl);
}

/* 列表中操作*/
//修改操作
var sceneAnchorData = {};
function modify(id,el){
    sceneAnchorData = JSON.parse($(el).attr("data-itemData"));
    var singleUrl = layer.open({
        type: 2,
        title: "修改锚点",
        content: 'sceneAnchorManagement.html?subSceneId='+subSceneId+'&operateType=edit'+'&id='+id+'&secondId='+secondId,
        end:function(){
            location.reload()
        }
    })
    layer.full(singleUrl);
}
// 删除操作
function cancel(id){
   getData.deleteAnchorInfo(id)
}